<?php
class wx_back extends CI_Controller {
	function valid() {
		$echoStr = $this -> input -> get('echostr');
		if ($this -> input -> get('echostr')) {
			if ($this -> checkSignature()) {
				echo $echoStr;
			}
		}
	}

	function checkSignature() {
		$signature = $this -> input -> get('signature');
		$timestamp = $this -> input -> get('timestamp');
		$nonce = $this -> input -> get('nonce');

		$token = $this -> config -> item('TOKEN');

		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr);
		$tmpStr = implode($tmpArr);
		$tmpStr = sha1($tmpStr);

		if ($tmpStr == $signature) {
			return true;
		} else {
			return false;
		}
	}

	function antiInjection($value) {
		// 反SQL注入
		if (get_magic_quotes_gpc())
			$value = stripslashes($value);
		$value = trim($value);
		$value = str_replace('＃', '#', $value);
		return $value;
	}

	function index() {
		$this -> valid();
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		if (!empty($postStr)) {
			$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
			$fromUsername = $postObj -> FromUserName;
			$toUsername = $postObj -> ToUserName;
			$keyword = trim($postObj -> Content);
			$time = time();

			$this -> load -> helper('string');

			$da['FromUserName'] = $this -> antiInjection($postObj -> FromUserName);
			$da['ToUserName'] = $this -> antiInjection($postObj -> ToUserName);
			$da['CreateTime'] = $this -> antiInjection($postObj -> CreateTime);
			$da['MsgType'] = $this -> antiInjection($postObj -> MsgType);
			$da['MsgId'] = $da['MsgType'] == 'event' ? random_string('md5') : $this -> antiInjection($postObj -> MsgId);

			$this -> db -> insert('wx', $da);
			$wx_id = $this -> db -> insert_id();

			if (!$wx_id) {
				exit();
			}

			$m = $this -> pub -> get_member($da['FromUserName']);
			if ($m['subscribe'] != 1) {
				$m = $this -> pub -> get_member($da['FromUserName'], TRUE);
			}

			switch($da['MsgType']) {
				case 'text' :
					$co['Content'] = $this -> antiInjection($postObj -> Content);
					break;
				case 'voice' :
					$co['MediaId'] = $this -> antiInjection($postObj -> MediaId);
					$co['Format'] = $this -> antiInjection($postObj -> Format);
					$co['Recognition'] = $this -> antiInjection($postObj -> Recognition);
					$co['MsgID'] = $this -> antiInjection($postObj -> MsgID);
					break;
				case 'video' :
					$co['MediaId'] = $this -> antiInjection($postObj -> MediaId);
					$co['ThumbMediaId'] = $this -> antiInjection($postObj -> ThumbMediaId);
					$co['MsgId'] = $this -> antiInjection($postObj -> MsgId);
					break;
				case 'shortvideo' :
					$co['MediaId'] = $this -> antiInjection($postObj -> MediaId);
					$co['ThumbMediaId'] = $this -> antiInjection($postObj -> ThumbMediaId);
					$co['MsgId'] = $this -> antiInjection($postObj -> MsgId);
					break;
				case 'image' :
					$co['PicUrl'] = $this -> antiInjection($postObj -> PicUrl);
					$co['MediaId'] = $this -> antiInjection($postObj -> MediaId);
					break;
				case 'location' :
					$co['Location_X'] = $this -> antiInjection($postObj -> Location_X);
					$co['Location_Y'] = $this -> antiInjection($postObj -> Location_Y);
					$co['Scale'] = $this -> antiInjection($postObj -> Scale);
					$co['Label'] = $this -> antiInjection($postObj -> Label);
					break;
				case 'link' :
					$co['Title'] = $this -> antiInjection($postObj -> Title);
					$co['Description'] = $this -> antiInjection($postObj -> Description);
					$co['Url'] = $this -> antiInjection($postObj -> Url);
					break;
				case 'event' :
					$co['Event'] = $this -> antiInjection($postObj -> Event);
					$co['EventKey'] = $this -> antiInjection($postObj -> EventKey);
					//客户关注的时候
					if ($co['Event'] == 'subscribe') {
						if ($co['EventKey']) {
							$ls = explode('_', $co['EventKey']);
							if ($ls[0] == 'qrscene') {
								$this -> cl_ewm($ls[1], $fromUsername, $toUsername, $time, $m);
								$this -> pub -> log_base('微信二维码关注' . $ls[1], $m['mid'], $m['nickname']);
							}
						} else {
							$this -> db -> select('title,pic,url,jj,wz_id');
							$this -> db -> where('is_wx', 1);
							$this -> db -> where('wz_del', 1);
							$q = $this -> db -> get_where('wz');
							if ($q -> num_rows() > 0) {
								$item = $q -> result_array();
								$this -> send_item($fromUsername, $toUsername, $time, $item);
							}
							$this -> pub -> log_base('微信检索关注' . $co['EventKey'], $m['mid'], $m['nickname']);
						}
					}
					if ($co['Event'] == 'unsubscribe') {
						$this -> db -> select('openid,nickname,headimgurl,mid');
						$q = $this -> db -> get_where('member', array('openid' => $da['FromUserName']));
						$m_fs = $q -> row_array(0);
						$this -> db -> delete('fs', array('mid' => $m_fs['mid']));
						$this -> pub -> log_base('微信取消关注', $m['mid'], $m['nickname']);
					}

					//关注后扫描二维码
					if ($co['Event'] == 'SCAN') {
						$this -> cl_ewm($co['EventKey'], $fromUsername, $toUsername, $time, $m);
					}
					if ($co['Event'] == 'LOCATION') {
						$co['Latitude'] = $this -> antiInjection($postObj -> Latitude);
						$co['Longitude'] = $this -> antiInjection($postObj -> Longitude);
						$co['Precision'] = $this -> antiInjection($postObj -> Precision);
					}
					//获取图片
					if ($co['Event'] == 'CLICK' && $co['EventKey'] == 'haibao') {
						if ($m['type'] == 3) {
							$this -> send_text($fromUsername, $toUsername, $time, '请点击如下链接获取二维码海报！<a href="' . $this -> config -> item('base_urlx') . '/shop/haibao/' . $m['mid'] . '">获取海报</a>');
						} else {
							$this -> send_text($fromUsername, $toUsername, $time, '您还没购买商业商品展柜，不能为您生成推广图片，<a href="' . $this -> config -> item('base_urlx') . '/?mid=' . $m['mid'] . '">立即点击.购买展柜</a>');
						}
						$this -> pub -> log_base('微信获取海报', $m['mid'], $m['nickname']);
					}
					//客服
					if ($co['Event'] == 'CLICK' && $co['EventKey'] == 'kefu') {
						$this -> send_text($fromUsername, $toUsername, $time, 'hi，请问有什么可以帮到你的？' . PHP_EOL . PHP_EOL . '点击左下角的小键盘回复需要咨询的内容，或直接拨打官方400电话：4008959508');
						$this -> pub -> log_base('客服', $m['mid'], $m['nickname']);
					}
					//获取链接
					if ($co['Event'] == 'CLICK' && $co['EventKey'] == 'hqll') {
						if ($m['type'] == 3) {
							$this -> send_text($fromUsername, $toUsername, $time, '恭喜你获得了全球独一无二的地址，玩命推广吧！' . $this -> config -> item('base_urlx') . '/?mid=' . $m['mid']);
						} else {
							$this -> send_text($fromUsername, $toUsername, $time, '您还不是东家，不能为您生成推广链接，<a href="' . $this -> config -> item('base_urlx') . '/?mid=' . $m['mid'] . '">立即点击.成为东家</a>');
						}
						$this -> pub -> log_base('微信获取链接', $m['mid'], $m['nickname']);
					}

					//签到
					if ($co['Event'] == 'CLICK' && $co['EventKey'] == 'qiandao') {
						$this -> db -> select('title,pic,url,jj,wz_id');
						$this -> db -> where('gjz', 'qd');
						$this -> db -> order_by('sort', 'asc');
						$this -> db -> where('wz_del', 1);
						$q = $this -> db -> get_where('wz');
						if ($q -> num_rows() > 0) {
							$item = $q -> result_array();
							$this -> send_item($fromUsername, $toUsername, $time, $item);
						}
						//签到写入数据库
						$qd['mid'] = $m['mid'];
						$qd['datex'] = date('Y-m-d');
						$qd['mktime'] = mktime();
						$this -> db -> insert('cj_qd', $qd);
					}

					//发送按钮事件
					if ($co['Event'] == 'CLICK' && $co['EventKey'] != '') {
						$this -> db -> select('title,pic,url,jj,wz_id');
						$this -> db -> like('gjz', $co['EventKey']);
						$this -> db -> where('wz_del', 1);
						$q = $this -> db -> get_where('wz');

						if ($q -> num_rows() > 0) {
							$item = $q -> result_array();
							$this -> send_item($fromUsername, $toUsername, $time, $item);
						} else {
							$this -> db -> where('gjz', $co['EventKey']);
							$this -> db -> where('gjz_del', 1);
							$this -> db -> where('state', 1);
							$q = $this -> db -> get_where('gjz');
							if ($q -> num_rows() > 0) {
								$ls = $q -> row_array(0);
								$this -> send_text($fromUsername, $toUsername, $time, $ls['content']);
							}
						}
						$this -> pub -> log_base('微信按钮获取内容' . $co['EventKey'], $m['mid'], $m['nickname']);
					}

					break;
			}
			$da['content'] = serialize($co);
			$this -> db -> update('wx', $da, array('wx_id' => $wx_id));

		} else {
			echo "";
			exit ;
		}
	}

	//获取二维码 1会员 2商品 3文字 4机构(扫一扫成为医生) 5登陆后台 6群组
	function cl_ewm($idx, $fromUsername, $toUsername, $time, $m) {
		$q = $this -> db -> get_where('ewm', array('ewm_id' => $idx));
		if ($q -> num_rows() > 0) {
			$ewm = $q -> row_array(0);
			switch($ewm['type']) {
				case 1 :
					$this -> db -> select('title,pic,url,jj,wz_id');
					$this -> db -> where('is_wx', 1);
					$this -> db -> where('wz_del', 1);
					$q = $this -> db -> get_where('wz');
					$item = $q -> result_array();

					$this -> db -> select('openid,nickname,headimgurl,mid');
					$q = $this -> db -> get_where('member', array('mid' => $ewm['dx_id']));
					$m_fs = $q -> row_array(0);

					//	$item[0]['url'] = site_url('?mid=' . $m_fs['mid']);
					//	$item[] = array('title' => '去逛逛' . $m_fs['nickname'] . '家的展品', 'pic' => $m_fs['headimgurl'], 'url' => $this -> config -> item('base_urlx'));
					$this -> send_text($fromUsername, $toUsername, $time, '欢迎光临'.$this->config->item('xt_name'));
					//$this -> send_item($fromUsername, $toUsername, $time, $item);
					//处理粉丝的情况
					$this -> db -> select('tj_mid');
					$q = $this -> db -> get_where('member', array('mid' => $m['mid']));
					$ms = $q -> row_array(0);
					//获取粉丝情况
					$sfpd = TRUE;
					if ($m['mid'] != $ewm['dx_id'] && $ms['tj_mid'] == $this -> config -> item('tj_mid')) {
						//判断粉丝的情况
						$sfpd = FALSE;
						$fs['tj_mid'] = $ewm['dx_id'];
						$this -> db -> limit(1);
						$this -> db -> update('member', $fs, array('mid' => $m['mid']));
						$this -> pub -> send_zd_text($m_fs['openid'], $m['nickname'] . '进入了您的团队！');
					}
					if ($sfpd) {
						$this -> pub -> send_zd_text($m_fs['openid'], $m['nickname'] . '的微信扫描了二维码！');
					}
					$this -> pub -> log_base('微信交互 扫描' . $m_fs['nickname'] . '的二维码', $m['mid'], $m['nickname']);
					break;
				case 2 :
					$this -> db -> select('name as title,pic1 as pic,sort as url,tj as jj,sp_id as wz_id');
					$this -> db -> where('sp_id', $ewm['dx_id']);
					$this -> db -> where('sp_del', 1);
					$q = $this -> db -> get_where('sp');
					$item = $q -> result_array();
					foreach ($item as $k => $v) {
						$item[$k]['url'] = $this -> config -> item('base_urlx') . '/cxt/sp/' . $v['wz_id'];
					}
					$this -> send_item($fromUsername, $toUsername, $time, $item);
					$this -> pub -> log_base('微信交互 扫描商品二维码', $m['mid'], $m['nickname']);
					break;
				case 3 :
					$this -> db -> select('title,pic,url,jj,wz_id');
					$this -> db -> where('wz_id', $ewm['dx_id']);
					$this -> db -> where('wz_del', 1);
					$q = $this -> db -> get_where('wz');
					$item = $q -> result_array();
					$this -> send_item($fromUsername, $toUsername, $time, $item);
					$this -> pub -> log_base('微信交互 扫描文章二维码', $m['mid'], $m['nickname']);
					break;
				case 4 :
					//更新门店管理员状态
					$this -> db -> where('mid', $m['mid']);
					$this -> db -> update('member', array('state' => 2, 'staff_id' => $ewm['dx_id']));
					$this -> db -> select('title,pic,url,jj,wz_id');
					$this -> db -> where('is_wx', 1);
					$this -> db -> where('wz_del', 1);
					$q = $this -> db -> get_where('wz');
					$item = $q -> result_array();
					$item[0]['url'] = $this -> config -> item('base_urlx') . '/?mid=' . $m_fs['mid'];

					//机构信息
					$this -> db -> select('jg_name');
					$q = $this -> db -> get_where('staff', array('staff_id' => $ewm['dx_id']));
					$staff = $q -> row_array(0);
					$item[] = array('title' => $staff['jg_name'], 'pic' => $this -> config -> item('base_urlx') . '/style/images/logo.png', 'url' => $this -> config -> item('base_urlx'), '');

					$this -> send_item($fromUsername, $toUsername, $time, $item);
					$this -> pub -> log_base('微信交互 扫描管理员二维码' . $staff['jg_name'], $m['mid'], $m['nickname']);
					break;
				case 5 :
					//订单提货
					$this -> db -> select('thshop,bill_id,name,mid,phone,state,mktime');
					$q = $this -> db -> get_where('bill', array('bill_id' => $ewm['dx_id']));
					$bill = $q -> row_array(0);

					$this -> db -> select('headimgurl,openid');
					$q = $this -> db -> get_where('member', array('mid' => $bill['mid']));
					$m_fs = $q -> row_array(0);

					if ($bill['thshop'] == $m['staff_id'] && $m['state'] == 2) {
						$item[0] = array('title' => $bill['name'] . '(' . $bill['phone'] . ')' . ' 订单提取', 'pic' => $m_fs['headimgurl'], 'url' => $this -> config -> item('base_urlx') . '/shop/bill');
						if ($bill['state'] == 3) {
							$item[] = array('title' => '商品已经在 ' . (@date('Y-m-d H:i', $bill['mktime'])) . '被提取', 'pic' => $this -> config -> item('base_urlx') . 'style/images/logo.png', 'url' => $this -> config -> item('base_urlx') . '/shop/bill_tq/' . $bill['bill_id']);
						} else {
							$item[] = array('title' => '提取商品', 'pic' => $this -> config -> item('base_urlx') . '/style/images/logo.png', 'url' => $this -> config -> item('base_urlx') . '/shop/bill_tq/' . $bill['bill_id']);
						}
						$this -> send_item($fromUsername, $toUsername, $time, $item);
						$this -> pub -> send_zd_text($m_fs['openid'], sprintf("%08d", $bill['bill_id']) . '订单的二维码扫描成功！');
					} else {
						$this -> send_text($fromUsername, $toUsername, $time, '您不是这家店的管理员，提货失败！');
					}
					$this -> pub -> log_base('微信交互 管理员提货', $m['mid'], $m['nickname']);
					break;
				case 6 :
					//报名
					$this -> db -> select('thshop,bill_id,name,mid,phone,state,mktime');
					$q = $this -> db -> get_where('bill', array('bill_id' => $ewm['dx_id']));
					$bill = $q -> row_array(0);

					$this -> db -> select('headimgurl,openid');
					$q = $this -> db -> get_where('member', array('mid' => $bill['mid']));
					$m_fs = $q -> row_array(0);

					//判断管理员是否是高级的
					$this -> db -> select('state');
					$q = $this -> db -> get_where('staff', array('staff_id' => $m['staff_id']));
					$staff = $q -> row_array(0);

					if ($staff['state'] == 2) {
						$item[0] = array('title' => $bill['name'] . '(' . $bill['phone'] . ')' . ' 订单提取', 'pic' => $m_fs['headimgurl'], 'url' => $this -> config -> item('base_urlx') . '/shop/bill');
						if ($bill['state'] == 3) {
							$item[] = array('title' => '报名的二维码在 ' . (@date('Y-m-d H:i', $bill['mktime'])) . '已被验证', 'pic' => $this -> config -> item('base_urlx') . 'style/images/logo.png', 'url' => $this -> config -> item('base_urlx') . '/shop/bill_bm/' . $bill['bill_id']);
						} else {
							$item[] = array('title' => '验证报名', 'pic' => $this -> config -> item('base_urlx') . '/style/images/logo.png', 'url' => $this -> config -> item('base_urlx') . '/shop/bill_bm/' . $bill['bill_id']);
						}
						$this -> send_item($fromUsername, $toUsername, $time, $item);
						$this -> pub -> send_zd_text($m_fs['openid'], sprintf("%08d", $bill['bill_id']) . '订单的二维码扫描成功！');
					} else {
						$this -> send_text($fromUsername, $toUsername, $time, '您不是高级管理员，报名验证失败！');
					}
					$this -> pub -> log_base('微信交互 管理员报名二维码', $m['mid'], $m['nickname']);
					break;
				case 7 :
					//管理员登陆
					$q = $this -> db -> get_where('staff', array('mid' => $m['mid']));
					$staff = $q -> row_array(0);
					if ($staff['state'] != 4 && $staff['type'] == 1) {
						$this -> db -> update('ewm_login', array('staff_id' => $staff['staff_id'], 'type' => 2), array('id' => $ewm['dx_id']));
						$this -> send_text($fromUsername, $toUsername, $time, '登录微信后端管理员系统成功！');
					} else {
						$this -> db -> update('ewm_login', array('staff_id' => $staff['staff_id'], 'type' => 3), array('id' => $ewm['dx_id']));
						$this -> send_text($fromUsername, $toUsername, $time, '您无权登录系统，请与管理员联系！管理员邮箱：a@72zu.com');
					}
					$this -> pub -> log_base('微信交互 管理员登录二维码', $m['mid'], $m['nickname']);
					break;
				case 8 :
					//群组
					$q = $this -> db -> get_where('co_main', array('id' => $ewm['dx_id']));
					$sq = $q -> row_array(0);

					$this -> db -> select('title,pic,url,jj,wz_id');
					$this -> db -> where('is_wx', 1);
					$this -> db -> where('wz_del', 1);
					$q = $this -> db -> get_where('wz');
					$item = $q -> result_array();

					$this -> db -> select('openid,nickname,headimgurl,mid');
					$q = $this -> db -> get_where('member', array('mid' => $sq['mid']));
					$m_fs = $q -> row_array(0);

					$item[] = array('title' => '去逛逛' . $m_fs['nickname'] . '家的微店', 'pic' => $m_fs['headimgurl'], 'url' => $this -> config -> item('base_urlx'));

					$item[] = array('title' => '收听' . $sq['name'], 'pic' => ($sq['pic'] ? ($this -> pub -> oss_dz($sq['pic'])) : $m_fs['headimgurl']), 'url' => $this -> config -> item('base_urlx') . '/wrq/main/' . $sq['gid']);

					$this -> send_item($fromUsername, $toUsername, $time, $item);

					//处理粉丝的情况
					$this -> db -> select('tj_mid');
					$q = $this -> db -> get_where('member', array('mid' => $m['mid']));
					$ms = $q -> row_array(0);
					//获取粉丝情况
					if ($m['mid'] != $sq['mid'] && $ms['tj_mid'] == $this -> config -> item('tj_mid')) {
						$fs['tj_mid'] = $sq['mid'];
						$this -> db -> limit(1);
						$this -> db -> update('member', $fs, array('mid' => $m['mid']));

						$this -> pub -> send_zd_text($m_fs['openid'], '恭喜！' . $m['nickname'] . '成为了您的粉丝！');

						if ($this -> config -> item('plug_tj_mid')) {
							$this -> pub -> send_zd_text($m['openid'], '恭喜您由【' . $m_fs['nickname'] . '】推荐成为【' . $this -> config -> item('xt_name') . '】的第' . $m['mid'] . '位粉丝！立即点击><a href=\"' . $this -> config -> item('base_urlx') . '/shop/bill' . '\">东家中心</a><马上抢占您的地盘');
						}

					}
					$this -> pub -> log_base('微信交互 扫描' . $m_fs['nickname'] . '的二维码', $m['mid'], $m['nickname']);

					break;
			}
		}
	}

	//转发信息
	function z_send($nr = '') {
		//获取全部的管理员账户
		$this -> db -> select('pan_member.openid,pan_member.mid,pan_member.nickname');
		$this -> db -> join('staff', 'pan_staff.staff_id=pan_member.staff_id');
		$this -> db -> where('pan_staff.state', 2);
		$q = $this -> db -> get_where('member');
		$staff = $q -> result_array();
		foreach ($staff as $v) {
			$this -> pub -> send_zd_text($v['openid'], $nr);
			$this -> pub -> log_base('转发 ' . $nr, $v['mid'], $v['nickname']);
		}
	}

	function send_text($fromUsername, $toUsername, $time, $contentStr = "Welcome to wechat world!") {
		$textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							</xml>";
		$msgType = "text";
		$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
		print $resultStr;
	}

	function send_img($fromUsername, $toUsername, $time, $media_id = "") {
		$textTpl = "<xml>
                              <ToUserName><![CDATA[%s]]></ToUserName>
                              <FromUserName><![CDATA[%s]]></FromUserName>
                              <CreateTime>%s</CreateTime>
                              <MsgType><![CDATA[%s]]></MsgType>
                              <Image>
                              <MediaId><![CDATA[%s]]></MediaId>
                              </Image>
                           </xml>";
		$msgType = "image";
		$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $media_id);
		echo $resultStr;
	}

	function send_item($fromUsername, $toUsername, $time, $items) {
		$da['FromUserName'] = $this -> antiInjection($fromUsername);
		$n = 0;
		$resultStr = '';
		foreach ($items as $v) {
			$textTpl = "
 <item>
 <Title><![CDATA[%s]]></Title> 
 <Description><![CDATA[%s]]></Description>
 <PicUrl><![CDATA[%s]]></PicUrl>
 <Url><![CDATA[%s]]></Url>
 </item>
 ";
			$v['url'] = $v['url'] ? $v['url'] : $this -> config -> item('base_urlx') . '/article/d/' . $v['wz_id'];
			$n++;
			$resultStr .= sprintf($textTpl, $v['title'], $v['jj'], strstr($v['pic'], 'http') ? $v['pic'] : ('http://' . $this -> config -> item('oss_path') . '.oss.aliyuncs.com/' . $v['pic']), $this -> antiInjection($v['url']));
		}
		$textTpl = " <xml>
 <ToUserName><![CDATA[%s]]></ToUserName>
 <FromUserName><![CDATA[%s]]></FromUserName>
 <CreateTime>%s</CreateTime>
 <MsgType><![CDATA[news]]></MsgType>
 <ArticleCount>" . $n . "</ArticleCount><Articles>";

		$resultStrx = sprintf($textTpl, $fromUsername, $toUsername, $time);
		$resultStrx .= $resultStr . "
 </Articles>
 </xml> ";
		echo $resultStrx;
	}

	function findNum($str = '') {
		$str = trim($str);
		if (empty($str)) {
			return '';
		}
		$result = '';
		for ($i = 0; $i < strlen($str); $i++) {
			if (is_numeric($str[$i])) {
				$result .= $str[$i];
			}
		}
		return $result;
	}

	function CheckTelephone($Argv) {
		$RegExp = '/^(?:13|15|18)[0-9]{9}$/';
		return preg_match($RegExp, $Argv) ? $Argv : false;
	}

	//转多客服
	function send_dkf($fromUsername, $toUsername, $time) {
		$textTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[transfer_customer_service]]></MsgType>
    <TransInfo>
         <KfAccount><![CDATA[%s]]></KfAccount>
     </TransInfo>
</xml>";

		$this -> db -> limit(1);
		$this -> db -> order_by('qz', 'asc');
		$q = $this -> db -> get_where('plug_kf', array('status >' => 0));
		if ($q -> num_rows() > 0) {
			$m = $q -> row_array(0);

			$this -> db -> update('plug_kf', array('qz' => ($m['qz'] + 1)), array('id' => $m['id']));

			$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $m['kf_account']);
			echo $resultStr;
		}

	}

	function send_dkf_zd($openid) {
		$this -> db -> limit(1);
		$this -> db -> order_by('qz', 'asc');
		$q = $this -> db -> get_where('plug_kf', array('status >' => 0));
		$m = $q -> row_array(0);

		$this -> db -> update('plug_kf', array('qz' => ($m['qz'] + 1)), array('id' => $m['id']));

		$access_token = $this -> pub -> access_token();
		$url = 'https://api.weixin.qq.com/customservice/kfsession/create?access_token=' . $access_token['access_token'];

		$data = '{"kf_account":"' . $m['kf_account'] . '","openid":"' . $openid . '","text":"新注册用户"}';
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$tmpInfo = curl_exec($ch);

		curl_close($ch);
	}






}
?>